﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Dazzle {
    public partial class GraphForm : Form {
        private ILog logger = LogManager.GetLogger(typeof(GraphForm));
        private GraphBox _box;
        private double _scale;
        //private bool _initializing = true;

        public GraphBox Box {
            get { return _box; }
        }

        //public GraphForm() {
        //    InitializeComponent();
        //    _box = new GraphBox("New form");
        //}

        public GraphForm(GraphBox box) {
            _box = box;
            InitializeComponent();

        }

        ~GraphForm()
        {
            logger.Debug("GraphForm destroyed");
        }

        private void GraphForm_FormClosing(object sender, FormClosingEventArgs e) {
            //if (Box.LastVerb == Win32.OLEIVERB_SHOW) {
            //    Box.Update();
            //}
            e.Cancel = true;
            Hide();
            Box.DoVerb(Win32.OLEIVERB_UPDATE, IntPtr.Zero, null, 0, IntPtr.Zero, new Win32.COMRECT());
            Box.DoVerb(Win32.OLEIVERB_HIDE, IntPtr.Zero, null, 0, IntPtr.Zero, new Win32.COMRECT());
        }

        private void SetClientSize() {
            ClientSize = new Size((int)Math.Round(Box.Extent.cx / _scale),
                                  (int)Math.Round(Box.Extent.cy / _scale));
        }

        public void BoxUpdated() {
            SetClientSize();
            propertyGrid.SelectedObject = Box.Chart;
        }

        private void GraphForm_Paint(object sender, PaintEventArgs e) {
            //Box.Renderer.RenderGraphics(e.Graphics, "Paint");
        }

        private void GraphForm_KeyDown(object sender, KeyEventArgs e) {
            if (e.Control && e.KeyCode == Keys.C)
                Clipboard.SetDataObject(Box);
        }

        private void GraphForm_Load(object sender, EventArgs e) {

            splitContainer1.Panel1.Controls.Clear();
            splitContainer1.Panel1.Controls.Add(Box.Chart);
            Box.Chart.Dock = DockStyle.Fill;

            using (Graphics g = CreateGraphics()) {
                _scale = 2540 / g.DpiX;
            }


            //MdiParent = Program.mainForm;

            logger.Debug("GraphForm(box):GraphType=" + Box.Chart.Name + ";Scale=" + _scale.ToString());

            //_initializing = false;

            BoxUpdated();

            Show();

            propertyGrid.SelectedObject = _box.Chart;
        }

        #region The file menu button events.

        private void tooltipSave_Click(object sender, EventArgs e) {
            Box.AdviseDataChanged();
        }

        #endregion
    }
}
